{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Doc Query Using Chroma\n",
    "\n",
    "`VectorstoreIndexCreator` already uses Chroma vectorstore by default (to be verified).\n",
    "\n",
    "In this example, the Chroma vectorstore is created and initialized from a document. \n",
    "\n",
    "Let us install/update the required packages.\n",
    "\n",
    "```python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "Requirement already satisfied: langchain in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (0.0.136)\n",
      "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (4.0.2)\n",
      "Requirement already satisfied: openapi-schema-pydantic<2.0,>=1.2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (1.2.4)\n",
      "Requirement already satisfied: requests<3,>=2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (2.28.2)\n",
      "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (0.5.7)\n",
      "Requirement already satisfied: PyYAML>=5.4.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (6.0)\n",
      "Requirement already satisfied: SQLAlchemy<2,>=1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (1.4.47)\n",
      "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (8.2.2)\n",
      "Requirement already satisfied: pydantic<2,>=1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (1.10.6)\n",
      "Requirement already satisfied: numpy<2,>=1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (1.23.5)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from langchain) (3.8.4)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.8.2)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (22.2.0)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n",
      "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (3.1.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\n",
      "Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (3.19.0)\n",
      "Requirement already satisfied: typing-inspect>=0.4.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (0.8.0)\n",
      "Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (1.5.1)\n",
      "Requirement already satisfied: typing-extensions>=4.2.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from pydantic<2,>=1->langchain) (4.5.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests<3,>=2->langchain) (2022.12.7)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests<3,>=2->langchain) (3.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests<3,>=2->langchain) (1.26.15)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from SQLAlchemy<2,>=1->langchain) (2.0.2)\n",
      "Requirement already satisfied: packaging>=17.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (23.0)\n",
      "Requirement already satisfied: mypy-extensions>=0.3.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (1.0.0)\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "Requirement already satisfied: openai in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (0.27.2)\n",
      "Collecting openai\n",
      "  Using cached openai-0.27.4-py3-none-any.whl (70 kB)\n",
      "Requirement already satisfied: requests>=2.20 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from openai) (2.28.2)\n",
      "Requirement already satisfied: aiohttp in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from openai) (3.8.4)\n",
      "Requirement already satisfied: tqdm in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from openai) (4.65.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests>=2.20->openai) (2022.12.7)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests>=2.20->openai) (1.26.15)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests>=2.20->openai) (3.4)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests>=2.20->openai) (3.1.0)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp->openai) (1.8.2)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp->openai) (22.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp->openai) (1.3.3)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp->openai) (4.0.2)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp->openai) (6.0.4)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from aiohttp->openai) (1.3.1)\n",
      "Installing collected packages: openai\n",
      "  Attempting uninstall: openai\n",
      "    Found existing installation: openai 0.27.2\n",
      "    Uninstalling openai-0.27.2:\n",
      "      Successfully uninstalled openai-0.27.2\n",
      "Successfully installed openai-0.27.4\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "Requirement already satisfied: unstructured in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (0.5.11)\n",
      "\u001b[33mWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError(\"HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)\")': /simple/unstructured/\u001b[0m\u001b[33m\n",
      "\u001b[0mRequirement already satisfied: python-magic in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (0.4.27)\n",
      "Requirement already satisfied: pypandoc in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (1.11)\n",
      "Requirement already satisfied: openpyxl in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (3.1.2)\n",
      "Requirement already satisfied: argilla in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (1.5.1)\n",
      "Requirement already satisfied: pandas in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (1.5.3)\n",
      "Requirement already satisfied: markdown in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (3.4.3)\n",
      "Requirement already satisfied: certifi>=2022.12.07 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (2022.12.7)\n",
      "Requirement already satisfied: pillow in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (9.4.0)\n",
      "Requirement already satisfied: msg-parser in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (1.2.0)\n",
      "Requirement already satisfied: python-pptx in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (0.6.21)\n",
      "Requirement already satisfied: nltk in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (3.8.1)\n",
      "Requirement already satisfied: requests in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (2.28.2)\n",
      "Requirement already satisfied: lxml in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (4.9.2)\n",
      "Requirement already satisfied: python-docx in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from unstructured) (0.8.11)\n",
      "Requirement already satisfied: httpx<0.24,>=0.15 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (0.23.3)\n",
      "Requirement already satisfied: backoff in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (2.2.1)\n",
      "Requirement already satisfied: rich<=13.0.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (13.0.1)\n",
      "Requirement already satisfied: tqdm>=4.27.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (4.65.0)\n",
      "Requirement already satisfied: deprecated~=1.2.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (1.2.13)\n",
      "Requirement already satisfied: wrapt<1.15,>=1.13 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (1.14.1)\n",
      "Requirement already satisfied: numpy<1.24.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (1.23.5)\n",
      "Requirement already satisfied: monotonic in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (1.6)\n",
      "Requirement already satisfied: packaging>=20.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (23.0)\n",
      "Requirement already satisfied: pydantic>=1.7.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from argilla->unstructured) (1.10.6)\n",
      "Requirement already satisfied: pytz>=2020.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from pandas->unstructured) (2022.7.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from pandas->unstructured) (2.8.2)\n",
      "Requirement already satisfied: olefile>=0.46 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from msg-parser->unstructured) (0.46)\n",
      "Requirement already satisfied: click in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from nltk->unstructured) (8.1.3)\n",
      "Requirement already satisfied: joblib in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from nltk->unstructured) (1.2.0)\n",
      "Requirement already satisfied: regex>=2021.8.3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from nltk->unstructured) (2022.10.31)\n",
      "Requirement already satisfied: et-xmlfile in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from openpyxl->unstructured) (1.1.0)\n",
      "Requirement already satisfied: XlsxWriter>=0.5.7 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from python-pptx->unstructured) (3.0.9)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests->unstructured) (1.26.15)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests->unstructured) (3.1.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests->unstructured) (3.4)\n",
      "Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from httpx<0.24,>=0.15->argilla->unstructured) (1.5.0)\n",
      "Requirement already satisfied: sniffio in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from httpx<0.24,>=0.15->argilla->unstructured) (1.3.0)\n",
      "Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from httpx<0.24,>=0.15->argilla->unstructured) (0.16.3)\n",
      "Requirement already satisfied: typing-extensions>=4.2.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from pydantic>=1.7.1->argilla->unstructured) (4.5.0)\n",
      "Requirement already satisfied: six>=1.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from python-dateutil>=2.8.1->pandas->unstructured) (1.16.0)\n",
      "Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from rich<=13.0.1->argilla->unstructured) (0.9.1)\n",
      "Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from rich<=13.0.1->argilla->unstructured) (2.14.0)\n",
      "Requirement already satisfied: anyio<5.0,>=3.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from httpcore<0.17.0,>=0.15.0->httpx<0.24,>=0.15->argilla->unstructured) (3.6.2)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from httpcore<0.17.0,>=0.15.0->httpx<0.24,>=0.15->argilla->unstructured) (0.14.0)\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "Requirement already satisfied: pypdf in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (3.7.1)\n",
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "Requirement already satisfied: chromadb in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (0.3.21)\n",
      "Requirement already satisfied: hnswlib>=0.7 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (0.7.0)\n",
      "Requirement already satisfied: requests>=2.28 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (2.28.2)\n",
      "Requirement already satisfied: pandas>=1.3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (1.5.3)\n",
      "Requirement already satisfied: pydantic>=1.9 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (1.10.6)\n",
      "Requirement already satisfied: sentence-transformers>=2.2.2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (2.2.2)\n",
      "Requirement already satisfied: duckdb>=0.7.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (0.7.1)\n",
      "Requirement already satisfied: fastapi>=0.85.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (0.88.0)\n",
      "Requirement already satisfied: numpy>=1.21.6 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (1.23.5)\n",
      "Requirement already satisfied: posthog>=2.4.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (2.4.2)\n",
      "Requirement already satisfied: clickhouse-connect>=0.5.7 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (0.5.16)\n",
      "Requirement already satisfied: uvicorn[standard]>=0.18.3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from chromadb) (0.21.1)\n",
      "Requirement already satisfied: pytz in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from clickhouse-connect>=0.5.7->chromadb) (2022.7.1)\n",
      "Requirement already satisfied: lz4 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from clickhouse-connect>=0.5.7->chromadb) (4.3.2)\n",
      "Requirement already satisfied: urllib3>=1.26 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from clickhouse-connect>=0.5.7->chromadb) (1.26.15)\n",
      "Requirement already satisfied: certifi in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from clickhouse-connect>=0.5.7->chromadb) (2022.12.7)\n",
      "Requirement already satisfied: zstandard in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from clickhouse-connect>=0.5.7->chromadb) (0.20.0)\n",
      "Requirement already satisfied: starlette==0.22.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from fastapi>=0.85.1->chromadb) (0.22.0)\n",
      "Requirement already satisfied: anyio<5,>=3.4.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from starlette==0.22.0->fastapi>=0.85.1->chromadb) (3.6.2)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from pandas>=1.3->chromadb) (2.8.2)\n",
      "Requirement already satisfied: backoff>=1.10.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from posthog>=2.4.0->chromadb) (2.2.1)\n",
      "Requirement already satisfied: monotonic>=1.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from posthog>=2.4.0->chromadb) (1.6)\n",
      "Requirement already satisfied: six>=1.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from posthog>=2.4.0->chromadb) (1.16.0)\n",
      "Requirement already satisfied: typing-extensions>=4.2.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from pydantic>=1.9->chromadb) (4.5.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests>=2.28->chromadb) (3.1.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from requests>=2.28->chromadb) (3.4)\n",
      "Requirement already satisfied: nltk in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (3.8.1)\n",
      "Requirement already satisfied: scikit-learn in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (1.2.2)\n",
      "Requirement already satisfied: torchvision in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (0.15.1)\n",
      "Requirement already satisfied: tqdm in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (4.65.0)\n",
      "Requirement already satisfied: scipy in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (1.10.1)\n",
      "Requirement already satisfied: torch>=1.6.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (2.0.0)\n",
      "Requirement already satisfied: sentencepiece in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (0.1.97)\n",
      "Requirement already satisfied: huggingface-hub>=0.4.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (0.13.3)\n",
      "Requirement already satisfied: transformers<5.0.0,>=4.6.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sentence-transformers>=2.2.2->chromadb) (4.27.2)\n",
      "Requirement already satisfied: h11>=0.8 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.14.0)\n",
      "Requirement already satisfied: click>=7.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (8.1.3)\n",
      "Requirement already satisfied: watchfiles>=0.13 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.18.1)\n",
      "Requirement already satisfied: websockets>=10.4 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (10.4)\n",
      "Requirement already satisfied: python-dotenv>=0.13 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (1.0.0)\n",
      "Requirement already satisfied: uvloop!=0.15.0,!=0.15.1,>=0.14.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.17.0)\n",
      "Requirement already satisfied: httptools>=0.5.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (0.5.0)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from uvicorn[standard]>=0.18.3->chromadb) (6.0)\n",
      "Requirement already satisfied: filelock in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence-transformers>=2.2.2->chromadb) (3.10.0)\n",
      "Requirement already satisfied: packaging>=20.9 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence-transformers>=2.2.2->chromadb) (23.0)\n",
      "Requirement already satisfied: nvidia-curand-cu11==10.2.10.91 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (10.2.10.91)\n",
      "Requirement already satisfied: nvidia-cusolver-cu11==11.4.0.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.4.0.1)\n",
      "Requirement already satisfied: nvidia-cudnn-cu11==8.5.0.96 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (8.5.0.96)\n",
      "Requirement already satisfied: nvidia-nvtx-cu11==11.7.91 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.7.91)\n",
      "Requirement already satisfied: jinja2 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (3.1.2)\n",
      "Requirement already satisfied: nvidia-nccl-cu11==2.14.3 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (2.14.3)\n",
      "Requirement already satisfied: nvidia-cuda-runtime-cu11==11.7.99 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.7.99)\n",
      "Requirement already satisfied: sympy in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (1.11.1)\n",
      "Requirement already satisfied: nvidia-cuda-cupti-cu11==11.7.101 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.7.101)\n",
      "Requirement already satisfied: nvidia-cublas-cu11==11.10.3.66 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.10.3.66)\n",
      "Requirement already satisfied: nvidia-cuda-nvrtc-cu11==11.7.99 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.7.99)\n",
      "Requirement already satisfied: nvidia-cufft-cu11==10.9.0.58 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (10.9.0.58)\n",
      "Requirement already satisfied: triton==2.0.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (2.0.0)\n",
      "Requirement already satisfied: networkx in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (3.0)\n",
      "Requirement already satisfied: nvidia-cusparse-cu11==11.7.4.91 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (11.7.4.91)\n",
      "Requirement already satisfied: wheel in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (0.40.0)\n",
      "Requirement already satisfied: setuptools in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (67.6.0)\n",
      "Requirement already satisfied: lit in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from triton==2.0.0->torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (15.0.7)\n",
      "Requirement already satisfied: cmake in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from triton==2.0.0->torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (3.26.0)\n",
      "Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers>=2.2.2->chromadb) (0.13.2)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers>=2.2.2->chromadb) (2022.10.31)\n",
      "Requirement already satisfied: joblib in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from nltk->sentence-transformers>=2.2.2->chromadb) (1.2.0)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from scikit-learn->sentence-transformers>=2.2.2->chromadb) (3.1.0)\n",
      "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from torchvision->sentence-transformers>=2.2.2->chromadb) (9.4.0)\n",
      "Requirement already satisfied: sniffio>=1.1 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from anyio<5,>=3.4.0->starlette==0.22.0->fastapi>=0.85.1->chromadb) (1.3.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from jinja2->torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (2.1.2)\n",
      "Requirement already satisfied: mpmath>=0.19 in /home/rowel/anaconda3/envs/llm/lib/python3.10/site-packages (from sympy->torch>=1.6.0->sentence-transformers>=2.2.2->chromadb) (1.3.0)\n"
     ]
    }
   ],
   "source": [
    "! pip install langchain --upgrade\n",
    "! pip install openai --upgrade\n",
    "! pip install unstructured --upgrade\n",
    "! pip install pypdf --upgrade\n",
    "! pip install chromadb --upgrade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import PyPDFLoader\n",
    "from langchain.vectorstores import Chroma\n",
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "import os\n",
    "import textwrap\n",
    "from langchain.llms import OpenAI"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since OpenAI embeddings will be used, the `OpenAIEmbeddings` instance is created. This requires that a valid API key is supplied."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = input(\"OpenAI API Key: \")\n",
    "os.environ[\"OPENAI_API_KEY\"] = query\n",
    "embeddings = OpenAIEmbeddings()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a Chroma vectorstore from a document (pdf)\n",
    "\n",
    "After initializing the OpenAI embeddings, the chroma vectorstore is created by loading a pdf document and parsing it into pages.\n",
    "\n",
    "In this example, the `metadata` is set to the document name and page number."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using embedded DuckDB without persistence: data will be transient\n",
      "No embedding_function provided, using default embedding function: SentenceTransformerEmbeddingFunction\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "UP_Academic_Information.pdf number of pages = 69\n"
     ]
    }
   ],
   "source": [
    "# sample pdf: https://ac.upd.edu.ph/acmedia/images/newpdfs/UP_Academic_Information.pdf\n",
    "\n",
    "pdf_url = input(\"Enter pdf url: \")\n",
    "loader = PyPDFLoader(pdf_url)\n",
    "document = loader.load()      \n",
    "pages = loader.load_and_split()\n",
    "\n",
    "# for the url, get the document name\n",
    "document_name = pdf_url.split(\"/\")[-1]\n",
    "document_len = len(pages)\n",
    "print(f\"{document_name} number of pages = {document_len}\")\n",
    "\n",
    "vectorstore = Chroma.from_documents(documents=pages, embeddings=embeddings, \\\n",
    "                                    metadatas=[{\"source\": f\"{document_name}:page-{i}\"} for i in range(document_len)])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create a QA Chain\n",
    "\n",
    "A chain for document question answering is created. This will be used to process queries about the document and pass them to the LLM to retrieve the answer.\n",
    "\n",
    "There are several chain_types. In this example, `map_rerank` is used to overcome the limitations of max tokens by breaking the document into chunks (eg pages) and reranking the results as returned by LLM. The result with the highest score is returned. \n",
    "\n",
    "More info can be found in the [LangChain documentation](https://python.langchain.com/en/latest/modules/chains/index_examples/qa_with_sources.html) and this [twitter post](https://twitter.com/wooing0306/status/1645092115914063872).\n",
    "\n",
    "```python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(temperature=0)\n",
    "\n",
    "from langchain.chains.qa_with_sources import load_qa_with_sources_chain\n",
    "chain = load_qa_with_sources_chain(llm, chain_type=\"map_rerank\",)\n",
    "\n",
    "# if w/o sources\n",
    "#from langchain.chains.question_answering import load_qa_chain\n",
    "#chain = load_qa_chain(llm, chain_type=\"stuff\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Human: : How many years to finish a bachelor's degree?\n",
      "AI:  Four years\n",
      "Human: : What is APE?\n",
      "AI:  APE stands for Advanced Placement Examinations and is usually a written\n",
      "examination that tests the student’s knowledge and skills of the course for\n",
      "which he/she would want advanced credits in.\n",
      "Human: : Are all freshman students required to take APE?\n",
      "AI:  No, APE is only for students who wish to be considered for advanced credits\n",
      "in a course.\n",
      "Human: : When does the first semester start?\n",
      "AI:  The first semester starts in August.\n",
      "Human: : How many weeks per semester?\n",
      "AI:  16 weeks\n",
      "Human: : bye\n",
      "AI: Bye!\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    input_prompt = \"Human: \"\n",
    "    query = input(input_prompt)\n",
    "\n",
    "    if query.lower() == \"bye\":\n",
    "        text = f\"{input_prompt}: {query}\"\n",
    "        print(textwrap.fill(text, width=80))\n",
    "        print(\"AI: Bye!\")\n",
    "        break\n",
    "\n",
    "    docs = vectorstore.similarity_search(query, include_metadata=True)\n",
    "    result = chain.run(input_documents=docs, question=query, return_sources=True)\n",
    "    text = f\"{input_prompt}: {query}\"\n",
    "    print(textwrap.fill(text, width=80))\n",
    "    text = f\"AI: {result}\"\n",
    "    print(textwrap.fill(text, width=80))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Human: : What is CRS?\n",
      "AI: {'answer': ' The Computerized Registration System (CRS) allows UP Diliman\n",
      "enrollees, faculty, and staff to access records online. It includes features for\n",
      "creating and updating student records during and after admission, encoding of\n",
      "new course offerings, submission of class schedules, pre-enlistment,\n",
      "registration, change of matriculation, dropping, online submission of grades,\n",
      "online viewing of grades, automated assessment of fees, tagging of\n",
      "scholarship/privileges (UP faculty/employees and dependents) and STFAP brackets,\n",
      "encoding of ineligibilities and accountabilities, students’ evaluation of\n",
      "teacher, management of enlistment priority, online advising, and generation of\n",
      "reports.', 'sources': ''}\n",
      "Human: : What is role of a registrar?\n",
      "AI: {'answer': ' The registrar is responsible for overseeing the registration\n",
      "process, including creating and updating student records, encoding of new course\n",
      "offerings, submission of class schedules, pre-enlistment, registration, change\n",
      "of matriculation, dropping, online submission of grades, online viewing of\n",
      "grades, automated assessment of fees, tagging of scholarship/privileges,\n",
      "encoding of ineligibilities and accountabilities, students’ evaluation of\n",
      "teacher, management of enlistment priority, online advising, and generation of\n",
      "reports.', 'sources': ''}\n",
      "Human: : bye\n",
      "AI: Bye!\n"
     ]
    }
   ],
   "source": [
    "# this is an alternative chain for QA with sources\n",
    "\n",
    "from langchain.chains import RetrievalQAWithSourcesChain\n",
    "\n",
    "chain = RetrievalQAWithSourcesChain.from_chain_type(OpenAI(temperature=0), chain_type=\"map_rerank\", \\\n",
    "    retriever=vectorstore.as_retriever())\n",
    "\n",
    "\n",
    "while True:\n",
    "    input_prompt = \"Human: \"\n",
    "    query = input(input_prompt)\n",
    "\n",
    "    if query.lower() == \"bye\":\n",
    "        text = f\"{input_prompt}: {query}\"\n",
    "        print(textwrap.fill(text, width=80))\n",
    "        print(\"AI: Bye!\")\n",
    "        break\n",
    "\n",
    "    result = chain({\"question\": query}, return_only_outputs=True)\n",
    "    text = f\"{input_prompt}: {query}\"\n",
    "    print(textwrap.fill(text, width=80))\n",
    "    text = f\"AI: {result}\"\n",
    "    print(textwrap.fill(text, width=80))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
